package jdbc;

import com.github.javafaker.Faker;
import net.bwie.realtime.jtp.HikaricpUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;

import java.sql.*;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.UUID;

public class EntranceGuardLogJDBC {
    public static void main(String[] args) {
        //设置MySQL参数
        String url = "jdbc:mysql://node101:3306/sca?rewriteBatchedStatements=true&characterEncoding=utf8";
        String user = "root";
        String password = "123456";
        Connection conn = null;
        PreparedStatement pstm =null;
        ResultSet rt = null;

        try {
            //获取MySQL 连接
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url, user, password);
            String sql = "INSERT INTO entrance_guard_log(id,openMode,openTime,ownerId) VALUES(?,?,?,?)";
            pstm = conn.prepareStatement(sql);

            //查询业主OwerId
            String hsql = "select id from ower";
            QueryRunner qr =  new QueryRunner(HikaricpUtils.getDataSource());
            List<Object[]> list = qr.query(hsql,new ArrayListHandler());


            Long startTime = System.currentTimeMillis();
            Faker fakerZH = new Faker(new Locale("zh-CN"));
            for (int i = 1; i <= 100; i++) {
                Thread.sleep(1000);
                pstm.setString(1, UUID.randomUUID().toString());
                pstm.setInt(2, fakerZH.number().numberBetween(1,5));
                pstm.setString(3, new Timestamp(System.currentTimeMillis())+"");

                //设置业主owerId
                Random random = new Random();
                int index = random.nextInt(list.size());
                Object[] objects=list.get(index);
                pstm.setInt(4, Integer.parseInt(objects[0].toString()));

                pstm.execute();
                //pstm.addBatch();
                System.out.println(UUID.randomUUID().toString()+","+fakerZH.number().numberBetween(1,5)+""+","+new Timestamp(System.currentTimeMillis())+","+fakerZH.number().numberBetween(10000000,10244818));
            }
            //pstm.executeBatch();
            Long endTime = System.currentTimeMillis();
            System.out.println("OK,用时：" + (endTime - startTime));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally{
            if(pstm!=null){
                try {
                    pstm.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        }
    }

}
